﻿using BCCommon;
using BCData.OA.WorkflowTemplate.WorkflowCategory;
using BCDto.OA.WorkflowTemplate.WorkflowCategory;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.EntityNameConst;
using BCEntity.OA.WorkflowTemplate.WorkflowCategory;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.OA.WorkflowTemplate.WorkflowCategory
{
    public class WorkflowCategoryService: IWorkflowCategoryService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWorkflowCategoryData workflowCategoryData;
        private readonly string key = string.Format("{0}", EntityNameConst.WorkflowCategoryEntity);

        public WorkflowCategoryService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWorkflowCategoryData workflowCategoryData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.workflowCategoryData = workflowCategoryData;
        }

        #region 检查工作流分类是否存在
        public bool Exists(long workflowCategoryId)
        {
            return this.workflowCategoryData.Exists(workflowCategoryId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加工作流分类
        public WorkflowCategoryDto Add(WorkflowCategoryRequestDto requestDto,AdminDto adminDto)
        {
            if (requestDto.CompanyId.HasValue)
            {
                if (this.workflowCategoryData.Exists(requestDto.CompanyId.Value, requestDto.CategoryName).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("此公司已存在此工作流分类，无法重复添加");
                }
            }
            else
            {
                if (this.workflowCategoryData.Exists(requestDto.CategoryName).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("此公司已存在此工作流分类，无法重复添加");
                }
            }
            WorkflowCategoryEntity entity = requestDto.As<WorkflowCategoryEntity>();
            entity.CreateAdminId = adminDto.AdminId;
            entity.CreateAdminName = adminDto.FullName;
            var result = this.workflowCategoryData.Add(entity).GetAwaiter().GetResult().As<WorkflowCategoryDto>();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 修改工作流分类
        public WorkflowCategoryDto Update(WorkflowCategoryPutDto putDto, AdminDto adminDto)
        {
            if (!this.Exists(putDto.WorkflowCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            if (putDto.CompanyId.HasValue)
            {
                if (this.workflowCategoryData.Exists(putDto.CompanyId.Value, putDto.CategoryName).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("此公司已存在此工作流分类，无法重复添加");
                }
            }
            else
            {
                if (this.workflowCategoryData.Exists(putDto.CategoryName).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("此公司已存在此工作流分类，无法重复添加");
                }
            }
            var entity = putDto.As<WorkflowCategoryEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.workflowCategoryData.Update(entity).GetAwaiter().GetResult().As<WorkflowCategoryDto>();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 删除工作流分类
        public bool Delete(long workflowCategoryId)
        {
            if (!this.Exists(workflowCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.workflowCategoryData.Delete(workflowCategoryId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 获取工作流分类
        public WorkflowCategoryDto Get(long workflowCategoryId)
        {
            string cacheKey = this.redisService.GetKey(key, workflowCategoryId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.workflowCategoryData.GetEntity(workflowCategoryId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromDays(1));
            return result.As<WorkflowCategoryDto>();
        }
        #endregion

        #region 获取工作流分类列表
        public IEnumerable<WorkflowCategoryDto> GetList(int? companyId)
        {
            string cacheKey = this.redisService.GetKey(key, companyId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.workflowCategoryData.GetList(companyId).GetAwaiter().GetResult();
            }, TimeSpan.FromDays(1));
            return result.As<IEnumerable<WorkflowCategoryDto>>();
        }
        #endregion

        public PageableList<WorkflowCategoryDto> Query(int? companyId, string categoryName, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, companyId,categoryName, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.workflowCategoryData.Query(companyId, categoryName, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WorkflowCategoryDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WorkflowCategoryDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));
            return result;
        }
    }
}
